package com.hotel.sampleTool;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.hotel.BL.POSCategoryLogic;
import com.hotel.BL.POSLogic;

import data.POS;
import data.POSCategory;

public class POSCategorySample extends RequestSampleData{
	public POSCategorySample(HttpServletRequest req,HttpServletResponse resp){
		this.req = req;
		this.resp = resp;
	}
	@Override
	public void CreateSample() {
		CreateSample(1);
	}

	@Override
	public void CreateSample(int iteration) {
		long hotelId = 1;
		POSCategoryLogic posCategoryLogic = new POSCategoryLogic(req,resp);
		List<POSCategory> ps = posCategoryLogic.GeneratePOSCategory(hotelId);
		for(POSCategory a : ps)
			datastore.put(a.getEntity());
		
		CreateiCoffeeSubCategory(hotelId);
		CreateiChicSubCategory(hotelId);
		
	}
	
	private void CreateiCoffeeSubCategory(long hotelId){
		POSCategoryLogic posCategoryLogic = new POSCategoryLogic(req,resp);
		POS pos = new POSLogic(req,resp).GetPOS(hotelId,"iCoffee");
		POSCategory iCoffeeCat = posCategoryLogic.GetPOSCategory(pos.getId(), "iCoffee");
		String iCoffeeSubCat = iCoffeeCat.getSubCategoryString();
		if(posCategoryLogic.GetPOSCategory(iCoffeeCat.getPOSId(),"iCoffee Frappe",iCoffeeCat.getId()) == null)
		{
			try{
				POSCategory item = new POSCategory();
				item.setHotelId(hotelId);
				item.setTitle("iCoffee Frappe");
				item.setPOSId(iCoffeeCat.getPOSId());
				item.setParentCategory(iCoffeeCat.getId());
				Key key = datastore.put(item.getEntity());
				item.setId(key.getId());
				item.setKey(key);
				if(iCoffeeSubCat != null && iCoffeeSubCat != "")
					iCoffeeSubCat += "," +  item.getId();
				else iCoffeeSubCat = Long.toString(item.getId());
			}catch(Exception e){}
		}
		if(posCategoryLogic.GetPOSCategory(iCoffeeCat.getPOSId(),"Hot iCoffee",iCoffeeCat.getId()) == null)
		{
			try{
				POSCategory item = new POSCategory();
				item.setHotelId(hotelId);
				item.setTitle("Hot iCoffee");
				item.setPOSId(iCoffeeCat.getPOSId());
				item.setParentCategory(iCoffeeCat.getId());
				Key key = datastore.put(item.getEntity());
				item.setId(key.getId());
				item.setKey(key);
				if(iCoffeeSubCat != null && iCoffeeSubCat != "")
					iCoffeeSubCat += "," +  item.getId();
				else iCoffeeSubCat = Long.toString(item.getId());
			}catch(Exception e){}
		}
		
		iCoffeeCat.setSubCategoryString(iCoffeeSubCat);
		posCategoryLogic.UpdatePOSCategory(iCoffeeCat);
	}
	
	private void CreateiChicSubCategory(long hotelId){
		POSCategoryLogic posCategoryLogic = new POSCategoryLogic(req,resp);
		POS pos = new POSLogic(req,resp).GetPOS(hotelId,"iChic");
		POSCategory employeeCat = posCategoryLogic.GetPOSCategory(pos.getId(), "Employee's Food");
		String employeeSubCat = employeeCat.getSubCategoryString();
		if(posCategoryLogic.GetPOSCategory(employeeCat.getPOSId(),"Fried Rice-",employeeCat.getId()) == null)
		{
			try{
				POSCategory item = new POSCategory();
				item.setHotelId(hotelId);
				item.setTitle("Fried Rice-");
				item.setPOSId(employeeCat.getPOSId());
				item.setParentCategory(employeeCat.getId());
				Key key = datastore.put(item.getEntity());
				item.setId(key.getId());
				item.setKey(key);
				if(employeeSubCat != null && employeeSubCat != "")
					employeeSubCat += "," + item.getId();
				else employeeSubCat = Long.toString(item.getId());
			}catch(Exception e){}
		}
		if(posCategoryLogic.GetPOSCategory(employeeCat.getPOSId(),"Omlette",employeeCat.getId()) == null)
		{
			try{
				POSCategory item = new POSCategory();
				item.setHotelId(hotelId);
				item.setTitle("Omlette");
				item.setPOSId(employeeCat.getPOSId());
				item.setParentCategory(employeeCat.getId());
				Key key = datastore.put(item.getEntity());
				item.setId(key.getId());
				item.setKey(key);
				if(employeeSubCat != null && employeeSubCat != "")
					employeeSubCat += "," + item.getId();
				else employeeSubCat = Long.toString(item.getId());
			}catch(Exception e){}
		}
		
		employeeCat.setSubCategoryString(employeeSubCat);
		posCategoryLogic.UpdatePOSCategory(employeeCat);
	}
}
